/**
 * 十一选五页面载入设定
 */
// 选择投注项是记录当前投注项的赔率
$singleOdds    = 0;
//新的容器，来存放用户有效的选择，即使切换玩法，依旧保存有效选择（有效选择不是有效投注
var packChoice = {};
// 拿到玩法数据准备载入玩法界面
$('.tag_lott_play_main').html(function (){
	var main_play_str = '';
	$.each(resData.playItem.mainItem, function (index, value){
		if(main_play_str){
			main_play_str += '<li class="tag_main_play_item" ami="' + index + '">' + value.play_name + '</li>';
		}else{
			main_play_str += '<li class="tag_main_play_item curr" ami="' + index + '">' + value.play_name + '</li>';
		}
	})
	return main_play_str;
});

// 设置主玩法点击切换
$('.tag_lott_play_main li').on('click', '', function (){
	// 点击更新主玩法内容上的样式切换
	$('.tag_main_play_item').removeClass('curr');
	$(this).addClass('curr');
	// 更新从玩法内容展示
	get_main_play_mode($(this).attr('ami'));
	// 默认从玩法内容加载
	get_follow_play_mode('');
})
// 设置从玩法点击切换
$('.tag_lott_play_follow').on('click', '.tag_follow_play_item', function (){
	// 样式切换
	$('.tag_follow_play_item').removeClass('curr');
	$(this).addClass('curr');
	get_follow_play_mode($(this).attr('ami'));
})
$('.tag_lottery_list').on('click', '.tag_lottery_item', function (){
	window.location = '/?m=index&c=syxw&id=' + $(this).attr('ami');
})

// 绑定投注选项的选择事件
$('.tag_play_content').on('click', '.tag_choice_item', function (){
	// 选择投注号时，清除追号记录
	if(!$.isEmptyObject(chaseData)){
		chaseData = {};
		ready_bet = {};
		$('.tag_chase_number').hide();
		$('.tag_order_bet').show();
		$('.checkedList tbody').html('');
		$('.tag_i_want_chase[ami="1"]').prop('checked', 'true');
	}
	
	var c_str  = $(this).attr('ami');
	var c_odds = $(this).attr('amw');
	if($(this).attr('class') == 'tag_choice_item curr'){
		// 移除选中状态
		$(this).removeClass('curr');
		if($(this).attr('group') == 1){
			betting[user_play].one.splice(($.inArray($(this).attr('ami'), betting[user_play].one)), 1);
		}else if($(this).attr('group') == 2){
			betting[user_play].two.splice(($.inArray($(this).attr('ami'), betting[user_play].two)), 1);
		}else if($(this).attr('group') == 3){
			betting[user_play].three.splice(($.inArray($(this).attr('ami'), betting[user_play].three)), 1);
		}
		oddsArr.splice($.inArray(c_odds, oddsArr), 1);
	}else{
		$(this).addClass('curr');
		if($(this).attr('group') == 1){
			betting[user_play].one.push(c_str);
		}else if($(this).attr('group') == 2){
			betting[user_play].two.push(c_str);
		}else if($(this).attr('group') == 3){
			betting[user_play].three.push(c_str);
		}
		$singleOdds = c_odds;
		oddsArr.push(c_odds);
	}
	check_valid_bet(this);
})

// 投注区域的功能性按键事件绑定设置
$('.tag_play_content').on('click', '.tag_func_btn', function (){
	var tmp = '';
	if($(this).attr('group') == 1){
		tmp = betting[user_play].one;
	}else if($(this).attr('group') == 2){
		tmp = betting[user_play].two;
	}else if($(this).attr('group') == 3){
		tmp = betting[user_play].three;
	}
	// 默认全部清除
	$('.tag_choice_item[group="' + $(this).attr('group') + '"]').removeClass('curr');
	var choiceList = '';
	switch($(this).attr('ami')){
		case 'all':
			// 该组全部点亮
			choiceList = $('.tag_choice_item[group="' + $(this).attr('group') + '"]');
			choiceList.addClass('curr');
			break;
		case 'da':
			if($('.tag_choice_item[group="' + $(this).attr('group') + '"]').length == 11){
				choiceList = $('.tag_choice_item[group="' + $(this).attr('group') + '"]:gt(4)');
			}else{
				choiceList = $('.tag_choice_item[group="' + $(this).attr('group') + '"]:gt(2)');
			}
			choiceList.addClass('curr');
			break;
		case 'xiao':
			if($('.tag_choice_item[group="' + $(this).attr('group') + '"]').length == 11){
				choiceList = $('.tag_choice_item[group="' + $(this).attr('group') + '"]:lt(5)');
			}else{
				choiceList = $('.tag_choice_item[group="' + $(this).attr('group') + '"]:lt(3)');
			}
			choiceList.addClass('curr');
			break;
		case 'ji':
			choiceList = $('.tag_choice_item[group="' + $(this).attr('group') + '"]:even');
			choiceList.addClass('curr');
			break;
		case 'ou':
			choiceList = $('.tag_choice_item[group="' + $(this).attr('group') + '"]:odd');
			choiceList.addClass('curr');
			break;
		default :
			break;
	}
	// 默认清除
	tmp.length     = 0;
	oddsArr.length = 0;
	if($(this).attr('ami') != 'qing'){
		// 重新装载预投注数据
		choiceList.each(function (){
			tmp.push($(this).attr('ami'));
			oddsArr.push($(this).attr('amw'));
		});
	}
	
	if($(this).attr('group') == 1){
		betting[user_play].one   = tmp;
	}else if($(this).attr('group') == 2){
		betting[user_play].two   = tmp;
	}else if($(this).attr('group') == 3){
		betting[user_play].three = tmp;
	}
	check_valid_bet('', $(this).attr('ami'));
	packChoice[user_play] = betting;
})

// 用户上传文件设定预投注内容
$('.tag_play_content').on('click', '.tag_import_betting', function (){
	$('.tag_import_filebtn').click();
})
// 用户上传操作
$('.tag_play_content').on('change', '.tag_import_filebtn', function (){
	var inputFile = $(this)[0].files[0];
	var reader    = new FileReader(); 
    reader.readAsText(inputFile); 
    reader.onload=function(f){ 
    	$('.tag_play_content .tag_import_content').val(this.result);
    	$('.tag_play_content .tag_import_tips').hide();
    	// 检查导入内容是否符合投注规范
    	check_import_betting();
    } 
})
// 用户手动修改上传内容
$('.tag_play_content').on('change', '.tag_import_content', function (){
	check_import_betting();
})

// 设置投注金额单位
var default_bet_money = 0;
var bet_money_unit    = 0;
$('.tag_bet_operation').on('change', '.tag_money_unit', function (){
	bet_jisuan();
})

// 减注
$('.tag_bet_operation').on('click', '.tag_betnum_subtract', function (){
	// 如果已经是0注，则不操作
	if($('.tag_betnum').val() == 0) { return; }
	$(this).removeClass('off');
	$('.tag_betnum').val(parseInt($('.tag_betnum').val())-1);
	if($('.tag_betnum').val() == 0) { $(this).addClass('off'); }
	bet_jisuan();
})
// 加注
$('.tag_bet_operation').on('click', '.tag_betnum_add', function (){
	$('.tag_betnum').val(parseInt($('.tag_betnum').val())+1);
	$('.tag_betnum_subtract').removeClass('off');
	bet_jisuan();
})

$('.tag_bet_operation').on('change', '.tag_betnum', function (){
	// 投注倍数改变，同步设置到当前的ready_bet中
	if($(this).val() == 0) {
		$(this).val() = 1;
		$('.tag_betnum_subtract').addClass('off');
	}else{
		$('.tag_betnum_subtract').removeClass('off');
	}
	bet_jisuan();
})

// 确认选号
$('.tag_bet_operation').on('click', '.tag_selection_bet', function (){
	if($.inArray(user_play, ['SM-QS_ZHI_XFS']) > -1 ){
		if($.isEmptyObject(betting[user_play].one)|| $.isEmptyObject(betting[user_play].two) || $.isEmptyObject(betting[user_play].three) ) {
			layer_msg('温馨提示', '投注失败，请选择投注号', '');
			return;
		}
	}
	
	create_bet_list();
	ready_bet_index++;
	// 重新载入投注部分
	get_follow_play_mode(play_follow.follow_name, true);
	$('.tag_order_bet').attr('d', '');
})
// 查看投注详细
$('.tag_checkList').on('click', '.tag_betting_detail', function (){
	var msg = '[' + resData.playItem.mainItem[$(this).attr('m')].play_name + ', ' + resData.playItem.mainItem[$(this).attr('m')].play[$(this).attr('f')].name + ']';
	msg    += $(this).attr('amv');
	layer_msg('温馨提示', msg);
})

$('.tag_checkList').on('click', '.tag_del_bet', function (){
	// 清除所针对的投注项
//	ready_bet[$(this).attr('ami')] = {};
	delete ready_bet[$(this).attr('ami')];
	// 刷新投注内容
	create_bet_list();
})

// =================================  导入框的功能按键   =============================
// 清空文本框：注明（浏览器不允许修改file控件的内容，所以这里清空内容并不能清空file控件的值，再次上传同一个文件并不会触发内容展示）
$('.tag_play_content').on('click', '.tag_import_func_clear', function (){
	$('.tag_play_content .tag_import_content').val('');
	$('.tag_play_content .tag_import_tips').show();
})
// 检查格式是否正确
$('.tag_play_content').on('click', '.tag_check_import', function (){
	check_bet_error();
})
// 删除格式不对的预投注项
$('.tag_play_content').on('click', '.tag_clear_error', function (){
	var flag = check_bet_error();
	if(flag == 'success') return;
	// 清除不正确的投注项， 并展示
	var bet  = '';
	$.each(ready_bet, function (i, v){
		if(v.check == 'success'){
			bet += v.bet.join(' ') + ',';
		}
	})
	$('.tag_import_content').val(bet.substr(0, bet.length - 1));
})

// ==================================================================================
var chaseData         = {};
var ready_chase_index = '';
$('.tag_i_want_chase').on('click', '', function(){
	if($(this).attr('ami') != 2){
		$('.tag_order_bet').show();
		$('.tag_chase_number').hide();
		return;
	}
	if($('.tag_checkList td').length < 1){
		layer_msg('温馨提示', '投注失败，请选择投注号', '');
		$('.tag_i_want_chase[ami="1"]').click();
		return;
	}
	
	var checkBlend = [];
	$.each(ready_bet, function (i, v){
		if($.inArray(v.bet_name, checkBlend) == -1 && v.bet_name != '' && v.bet_name != 'undefined'){
			checkBlend.push(v.bet_name);
		}
	})
	if(checkBlend.length > 1){
		layer_msg('温馨提示', '追号不支持混投，请确保您的投注都为同一玩法且元角分模式一致。');
		$('.tag_i_want_chase[ami="1"]').click();
		return;
	}
	$.each(ready_bet, function (i, v){
		ready_chase_index = i;
		return true;
	})
	
	chaseData.chaseType = '0';
	chaseData.qishu     = [];
	chaseData.winnerEnd = '0';
	chaseData.lott_id   = resData.lotteryInfo.id;
	$.each(chaseData, function (i, v){
		chaseData.betting = v.betting;
		// 跳出循环
		return false;
	})
	$('.tag_chase_show_list').eq(0).html(chase_main_1($('#tag_set_chase_qishu').val(), $('.tag_all_chase').eq(0).val(), $('#tag_set_chase_beishu').val()));
	$('.tag_chase_number').show();
	$('.tag_order_bet').hide();
	// 展示追号统计信息，准备提交数据
	chase_tongji(chaseData.chaseType);
})
// 追号类型选择
$('.tag_chase_type li').on('click', '', function (){
	chaseData.chaseType = $(this).index();
	// 初始化选项卡的样式
	$('.tag_chase_type li').removeClass('curr');
	// 当前点击选中
	$(this).addClass('curr');
	// 获取当前点击选项卡的下标，打开内容区域
	$('.tag_chase_plan div').hide();
	// 追号信息内容展示区域
	$('.tag_chase_list').hide();
	$('.tag_chase_list').eq($(this).index()).show();
	// 填充对应区域
	var show_list = '';
	switch($(this).index().toString()){
		case '0':
			// 追号信息展示
			$('#tag_create_chase_plan').hide();
			show_list   = $('.tag_chase_show_list').eq(0);
			show_list.html(chase_main_1($('#tag_set_chase_qishu').val(), $('.tag_all_chase').eq(0).val(), $('#tag_set_chase_beishu').val()));
			break;
		case '1':

			//chase_second();

			$('#tag_create_chase_plan').show();
			// 追号信息展示
			show_list   = $('.tag_chase_show_list').eq(1);
			show_list.html(chase_main_1($('#tag_chase_fanbei_qishu').val(), $('.tag_all_chase').eq(1).val(), $('#tag_chase_fanbei_qsbeishu').val(), true));
			break;
		case '2':
			break;
		case '3':
			break;
		default:
			break;
	}
	// 展示对应区域
	$('.tag_chase_plan div').eq($(this).index()).show();
	// 展示追号统计信息，准备提交数据
	chase_tongji(chaseData.chaseType);
})
// 选择追号期数数量
$('.tag_chase_nums li').on('click', '', function(){
	$('.tag_chase_nums li').removeClass('curr');
	$(this).addClass('curr');
	$('#tag_set_chase_qishu').val($('.tag_chase_nums li[class="curr"]').attr('ami'));
	$('.tag_chase_show_list').eq(0).html(chase_main_1($(this).attr('ami'), $('.tag_all_chase').eq(0).val(), $('#tag_set_chase_beishu').val()));
})
// 全选，全不选
$('.tag_all_chase').on('click', '', function(){
	if($(this).prop('checked')){
		$('.tag_main_chase_preiod[group="' + $(this).attr('group') + '"]').attr('checked', true);
		var tmp = [];
		$.each($('.tag_main_chase_preiod[group="' + $(this).attr('group') + '"]'), function (i, v){
			if($(v).prop('checked')){
				tmp.push($(v).attr('ami'));
			}
		})
		chaseData.qishu = tmp;
	}else{
		$('.tag_main_chase_preiod[group="' + $(this).attr('group') + '"]').removeAttr('checked');
		chaseData.qishu = [];
	}
	chase_tongji(chaseData.chaseType.toString());
})
$('.tag_chase_show_list').on('click', '.tag_main_chase_preiod', function (){
	var tmp = [];
	$.each($('.tag_main_chase_preiod[group="' + $(this).attr('group') + '"]'), function (i, v){
		if($(v).prop('checked')){
			tmp.push($(v).attr('ami'));
		}
	})
	chaseData.qishu = tmp;
	chase_tongji($(this).attr('group'));
	if($(this).prop('checked') == false){
		var id = '#tag_main_chase_item_' + $(this).attr('group') + '_' + $(this).attr('amv');
		$(id).html('￥ 0.00元');
		$(id).attr('amv', '0.00');
	}
})


// 手动设置追号数
$('#tag_set_chase_qishu').on('change', '', function (){
	if($(this).val() < 0 ) $(this).val(10);
	$('.tag_chase_show_list').eq(0).html(chase_main_1($(this).val(), $('.tag_all_chase').eq(0).val(), $('#tag_set_chase_beishu').val()));
})
// 手动设置倍数
$('#tag_set_chase_beishu').on('change', '', function (){
	if($(this).val() <= 0){
		$(this).val('1');
	}
	$('.tag_main_chase_beishu').val($(this).val());
	// 更新追号的金额信息
	$.each($('.tag_main_chase_beishu'), function (i,v){
//		$('#tag_main_chase_item_' + $(v).attr('ami')).html('￥' + ($(v).val() * $('#tag_main_chase_item_' + $(v).attr('ami')).attr('amv')).toFixed(2) + '元');
		$('#tag_main_chase_item_' + $(this).attr('group') + '_' + $(this).attr('ami')).html(
				'￥' + 
				($(this).val() * $('#tag_main_chase_item_' + $(this).attr('group') + '_' + $(this).attr('ami')).attr('amv')).toFixed(2) + 
				'元');
	})
	chase_tongji(chaseData.chaseType.toString());
})

// 设置翻倍期数
$('#tag_chase_fanbei_qishu').on('change', '', function (){
	$('.tag_chase_show_list').eq(1).html(chase_main_1($('#tag_chase_fanbei_qishu').val(), $('.tag_all_chase').eq(1).val(), $('#tag_chase_fanbei_qsbeishu').val(), true));
})

// 根据当前选择追号方式生成追号计划
$('#tag_create_chase_plan').on('click', '', function (){
	switch(chaseData.chaseType.toString()){
		case '0':
			break;
		case '1':
			create_plan2('fanbeiPlan');
			break;
		case '2':
			break;
		case '3':
			break;
	}
	chase_tongji(chaseData.chaseType.toString());
})
// 修改倍数，更细统计信息
$('.tag_chase_list').on('change', '.tag_main_chase_beishu', function (){
	$('#tag_main_chase_item_' + $(this).attr('group') + '_' + $(this).attr('ami')).html(
			'￥' + 
			($(this).val() * $('#tag_main_chase_item_' + $(this).attr('group') + '_' + $(this).attr('ami')).attr('amv')).toFixed(2) + 
			'元');
	chase_tongji(chaseData.chaseType.toString());
})
// 停止追号设置
$('#id_stop_chase').on('click', '', function (){
	if($(this).prop('checked')){
		chaseData.winnerEnd = 1;
	}else{
		chaseData.winnerEnd = 0;
	}
})

// 马上投注（普通投注，不追号）
$('.tag_order_bet').on('click', '', function(){
	// 如果没有确认选项，则不能提交
	if($('.tag_checkList').html() == ''){
		layer_msg('温馨提示', '投注失败，请选择投注号', '');
		return;
	}
	showBet(this, '');
	return;
//	bet_post(this, '');
})
// 立即投注（追号投注
$('#id_bet_sub').on('click', '', function (){
	showBet(this, 'chase');
	return;
//	bet_post(this, 'chase');
})

//禁止左右切换
 $(window).load(function(){ 
	var len = $(".betNav li").length;
	//首次加载通过判断len来是否禁止左右按钮点击
	if(len<6){
		$(".betNavtab.left").addClass("betNavtabClass");
		$(".betNavtab.right").addClass("betNavtabClass");
	}
})

$('.tag_chase_fanbei').html(function (){
	setChaseBeginPreiod();
});

$('.tag_chase_fanbei').on('click', '', function (){
	show_list   = $('.tag_chase_show_list').eq(1);
	show_list.html(chase_main_1($('#tag_chase_fanbei_qishu').val(), $('.tag_all_chase').eq(1).val(), $('#tag_chase_fanbei_qsbeishu').val(), true));
})

// 有些玩法中的赔率信息是悬浮展示，移开关闭，针对那些一个玩法有多种赔率的情况
$('.tag_play_explain').on('mouseover', '.tag_winner_explain', function (){
	$('.hoverContent').show();
})
